﻿$.extend({
    imgLoad: function (options) {
        options = $.extend({
            container: "img",//集合
            time: 100,//滚动时定时刷新时间
            attribute: "src2" //保存原图地址的自定义属性
        }, options);
        var container = options.container, n = n || 0, tim, _time = 0;
        Load();
        function GetHeight() {
            var d = document,
                y = (navigator.userAgent.toLowerCase().match(/iPad/i) == "ipad") ? window.pageYOffset : Math.max(d.documentElement.scrollTop, d.body.scrollTop);
            return d.documentElement.clientHeight + y - n;
        }
        function Load() {
            var hg = GetHeight();
            $(container).each(function () {
                var _s2 = $(this).attr(options.attribute), t = $(this).offset().top;
                if (_s2 && t < hg && t > hg - 1000) {
                    $(this).attr("src", _s2).removeAttr(options.attribute)
                };
            })
        };
        if (!!window.ActiveXObject && !window.XMLHttpRequest)
        { _time = options.time }
        $(window).scroll(function () {
            clearTimeout(tim);
            tim = setTimeout(function () { Load(); }, options.time);
        });


    },
    lzAjax: function (url, data, callback) {
        $.ajax({
            type: "POST",
            url: url,
            data: data == null ? "{}" : $.toJSON(data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                callback(result.d)
            },
            error: function (result) {
            }
        });
    },
    request: function (paras) {
        var url = location.href;
        var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
        var paraObj = {}
        for (i = 0; j = paraString[i]; i++) {
            paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
        }
        var returnValue = paraObj[paras.toLowerCase()];
        if (typeof (returnValue) == "undefined") {
            return "";
        } else {
            return returnValue;
        }
    },
    format: function (source, params) {
        if (arguments.length == 1)
            return function () {
                var args = $.makeArray(arguments);
                args.unshift(source);
                return $.format.apply(this, args);
            };
        if (arguments.length > 2 && params.constructor != Array) {
            params = $.makeArray(arguments).slice(1);
        }
        if (params.constructor != Array) {
            params = [params];
        }
        $.each(params, function (i, n) {
            source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
        });
        return source;
    },
    cookie: function (name, value, options) {
        if (typeof value != 'undefined') {
            options = options || {};
            if (value === null) {
                value = '';
                options = $.extend({}, options);
                options.expires = -1;
            }
            var expires = '';
            if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                var date;
                if (typeof options.expires == 'number') {
                    date = new Date();
                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                } else {
                    date = options.expires;
                }
                expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
            }
            var path = options.path ? '; path=' + (options.path) : '';
            var domain = options.domain ? '; domain=' + (options.domain) : '';
            var secure = options.secure ? '; secure' : '';
            document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
        } else {
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
    },
    isNull: function (value) {
        return value == 'undefined' ? "" : value;
    },
    imgPath: "http://localhost:31925/"
});
$.fn.extend({
    tipError: function (text) {
        var $item = $(this);
        $item.poshytip({
            content: text,
            className: 'tip-error',
            showOn: 'none',
            alignTo: 'target',
            alignX: 'inner-left',
            offsetX: 0,
            offsetY: 5
        });
        $item.poshytip('show');
        $item.focus();
        $item.live('focusout', function (event) {
            $item.poshytip('hide');
        });

    },
    jqueryzoom: function (options) {
        var settings = {
            xzoom: 200,
            yzoom: 200,
            offset: 10,
            position: "right",
            lens: 1,
            preload: 1
        };
        if (options) {
            $.extend(settings, options)
        }
        var noalt = '';
        $(this).hover(function () {
            var imageLeft = $(this).offset().left;
            var imageTop = $(this).offset().top;
            var imageWidth = $(this).children('img').get(0).offsetWidth;
            var imageHeight = $(this).children('img').get(0).offsetHeight;
            noalt = $(this).children("img").attr("alt");
            var bigimage = $(this).children("img").attr("zoompic");
            $(this).children("img").attr("alt", '');
            if ($("div.zoomdiv").get().length == 0) {
                $(this).after("<div class='zoomdiv'><img class='bigimg' src='"
      + bigimage + "'/></div>");
                $(this).append("<div class='jqZoomPup'>&nbsp;</div>")
            }
            if (settings.position == "right") {
                if (imageLeft + imageWidth + settings.offset + settings.xzoom > screen.width) {
                    leftpos = imageLeft - settings.offset - settings.xzoom
                } else {
                    leftpos = imageLeft + imageWidth + settings.offset
                }
            } else {
                leftpos = imageLeft - settings.xzoom - settings.offset;
                if (leftpos < 0) {
                    leftpos = imageLeft + imageWidth + settings.offset
                }
            }
            $("div.zoomdiv").css({
                top: imageTop,
                left: leftpos
            });
            $("div.zoomdiv").width(settings.xzoom);
            $("div.zoomdiv").height(settings.yzoom);
            $("div.zoomdiv").show();
            if (!settings.lens) {
                $(this).css('cursor', 'crosshair')
            }
            $(document.body).mousemove(function (e) {
                mouse = new MouseEvent(e);
                var bigwidth = $(".bigimg").get(0).offsetWidth;
                var bigheight = $(".bigimg").get(0).offsetHeight;
                var scaley = 'x';
                var scalex = 'y';
                if (isNaN(scalex) | isNaN(scaley)) {
                    var scalex = (bigwidth / imageWidth);
                    var scaley = (bigheight / imageHeight);
                    $("div.jqZoomPup").width((settings.xzoom) / scalex);
                    $("div.jqZoomPup").height((settings.yzoom) / scaley);
                    if (settings.lens) {
                        $("div.jqZoomPup").css('visibility', 'visible')
                    }
                }
                xpos = mouse.x - $("div.jqZoomPup").width() / 2 - imageLeft;
                ypos = mouse.y - $("div.jqZoomPup").height() / 2 - imageTop;
                if (settings.lens) {
                    xpos = (mouse.x - $("div.jqZoomPup").width() / 2 < imageLeft)
       ? 0
       : (mouse.x + $("div.jqZoomPup").width() / 2 > imageWidth
         + imageLeft) ? (imageWidth
         - $("div.jqZoomPup").width() - 2) : xpos;
                    ypos = (mouse.y - $("div.jqZoomPup").height() / 2 < imageTop)
       ? 0
       : (mouse.y + $("div.jqZoomPup").height() / 2 > imageHeight
         + imageTop) ? (imageHeight
         - $("div.jqZoomPup").height() - 2) : ypos
                }
                if (settings.lens) {
                    $("div.jqZoomPup").css({
                        top: ypos,
                        left: xpos
                    })
                }
                scrolly = ypos;
                $("div.zoomdiv").get(0).scrollTop = scrolly * scaley;
                scrollx = xpos;
                $("div.zoomdiv").get(0).scrollLeft = (scrollx) * scalex
            })
        }, function () {
            $(this).children("img").attr("alt", noalt);
            $(document.body).unbind("mousemove");
            if (settings.lens) {
                $("div.jqZoomPup").remove()
            }
            $("div.zoomdiv").remove()
        });
        count = 0;
        if (settings.preload) {
            $('body').append("<div style='display:none;' class='jqPreload"
     + count + "'>sdsdssdsd</div>");
            $(this).each(function () {
                var imagetopreload = $(this).children("img").attr("zoompic");
                var content = jQuery('div.jqPreload' + count + '').html();
                jQuery('div.jqPreload' + count + '').html(content
      + '<img src=\"' + imagetopreload + '\">')
            })
        }
    }
});
function MouseEvent(e) {
    this.x = e.pageX;
    this.y = e.pageY
}
 
